package com.zjitc.service.impl;

import com.zjitc.common.ServerResponse;
import com.zjitc.dao.UserDao;
import com.zjitc.model.User;
import com.zjitc.service.UserService;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/4/16 15:53
 * @description:
 */
@Service
public class UserServiceImpl implements UserService {

  @Autowired
  private UserDao userDao;

  @Override
  public ServerResponse<User> login(@RequestParam("username") String username, @RequestParam("password") String password) {
    int count = userDao.checkUser(username);
    if (count != 1) {
      return ServerResponse.createError("用户不存在");
    }
    User user = userDao.findByUsernameAndPassword(username, password);
    if (user == null) {
      return ServerResponse.createError("密码错误");
    }
    user.setQuestion(null);
    user.setAnswer(null);
    return ServerResponse.createSucess(user);
  }

  @Override
  public ServerResponse<User> register(User user) {
    int count = userDao.checkUser(user.getUsername());
    if (count == 1) {
      return ServerResponse.createError("用户已存在");
    }
     userDao.register(user);
    return ServerResponse.createSucess("校验成功");
  }

  @Override
  public ServerResponse<User> updateInformation(User user) {
    int count = userDao.updateUser(user.getUsername(),user.getPassword(),user.getPhone(),user.getAddress());
    if(count>0){
      return ServerResponse.createSucess("更新个人信息成功");
    }
    return ServerResponse.createError("更新个人信息失败");
  }

}
